package com.zhangtai.modules.service.impl.employee;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dao.EmployeeRecruitmentDao;
import com.zhangtai.modules.entity.employee.EmployeeInfoEntity;
import com.zhangtai.modules.entity.employee.EmployeeRecruitmentEntity;
import com.zhangtai.modules.service.employee.EmployeeRecruitmentService;
import com.zhangtai.modules.vo.RegularVo;
import com.zhangtai.utils.DateUtils;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
public class EmployeeRecruitmentServiceImpl extends ServiceImpl<EmployeeRecruitmentDao, EmployeeRecruitmentEntity> implements EmployeeRecruitmentService {

    @Autowired
    private  EmployeeRecruitmentDao employeeRecruitmentDao;

    @Override
    public PageResult queryPage(
                                @RequestParam(value = "name", required = false) String name,
                                @RequestParam(value = "orgname", required = false) List<Long> orgname,
                                @RequestParam(value = "unitname", required = false) List<Long> unitname,
                                @RequestParam(value = "jobname", required = false) List<Long> jobname,
                                @RequestParam(value = "status",required = false)List<Integer> status,
                                @RequestParam(value = "isEntryStatus", required = false) Integer isEntryStatus,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
                                ,@RequestParam(value = "orgIds") List<Long> orgIds,
                                @RequestParam(value = "socialTrainee", required = false) Integer socialTrainee ,
                                @RequestParam(value="isOffer",required = false)      Integer isOffer,
                                @RequestParam(value="isSendContract",required = false)      Integer isSendContract,
                                @RequestParam(value="userCode",required = false)      String  userCode
    ) {
        QueryWrapper<EmployeeRecruitmentEntity> employeeWrapper =  new QueryWrapper<EmployeeRecruitmentEntity>()
                .like(!StringUtils.isEmpty(name),"name", name).in(!CollectionUtils.isEmpty(orgname),"org_id", orgname)
                .eq(orgname!=null&&orgname.size()==0,"org_id", null).eq(unitname!=null&&unitname.size()==0,"unit_id", unitname)
                .eq(jobname!=null&&jobname.size()==0,"job_id", null)
                .in(!CollectionUtils.isEmpty(unitname),"unit_id", unitname).in(!CollectionUtils.isEmpty(jobname),"job_id", jobname)
                .eq("is_delete",0)
                .eq(isEntryStatus!=null,"is_entry_status",isEntryStatus)
                .eq(!ObjectUtils.isEmpty(socialTrainee),"social_trainee",socialTrainee)
                .in(!CollectionUtils.isEmpty(status),"status",status)
                .eq(isOffer!=null,"is_offer",isOffer)
                .eq(!ObjectUtils.isEmpty(isSendContract),"is_send_contract",isSendContract);
        employeeWrapper.and(wra->wra.in(!CollectionUtils.isEmpty(orgIds),"unit_id",orgIds).or().
                eq("principal_user_code",userCode).or().eq(CollectionUtils.isEmpty(orgIds),"unit_id",null)).orderByDesc("hiredate");
        IPage<EmployeeRecruitmentEntity> page = this.page(
                new Page<EmployeeRecruitmentEntity>(pageNum,pageSize),employeeWrapper
        );

        return new PageResult(page);
    }


    @Override
    public PageResult<EmployeeRecruitmentEntity> getRegularListPage(@RequestBody RegularVo regularVo, List<Long> lineSystem,
                                                         List<Long> jobSystem, List<Long> orgIds,
                                                         List<Long> org, List<Long> unit, List<Long> job) {

        IPage<EmployeeRecruitmentEntity> page = this.page(
                new Page<EmployeeRecruitmentEntity>(regularVo.getPageNum(),regularVo.getPageSize()),
                //is_reinstate 0:新员工，1离职未满一年，3离职超过一年   ，social_trainee：0社招
                getQuery(regularVo,lineSystem,jobSystem,orgIds,org,unit,job)
        );

        return new PageResult(page);

    }



    @Override
    public List<EmployeeRecruitmentEntity> getRegularList(@RequestBody RegularVo regularVo, List<Long> lineSystem,
                                                                    List<Long> jobSystem, List<Long> orgIds,
                                                                    List<Long> org, List<Long> unit, List<Long> job) {
        return   employeeRecruitmentDao.selectList( getQuery(regularVo,lineSystem,jobSystem,orgIds,org,unit,job));

    }

    public  QueryWrapper<EmployeeRecruitmentEntity> getQuery(@RequestBody RegularVo regularVo, List<Long> lineSystem,
                                                             List<Long> jobSystem, List<Long> orgIds,
                                                             List<Long> org, List<Long> unit, List<Long> job){

        String name = regularVo.getName();
        List<Integer> isRegular = regularVo.getIsRegular();
        Integer dealStatus = regularVo.getDealStatus();
        Integer isDistribute = regularVo.getIsDistribute();
        Boolean flag = regularVo.getFlag();
//        String jobName = regularVo.getJobName();
        List<Integer> regularBpmStatus = regularVo.getRegularBpmStatus();
        ArrayList<Integer> integers = new ArrayList<>();
        integers.add(0);
        integers.add(1);
        integers.add(3);
        ArrayList<Integer> jobTypeNot = new ArrayList<>();
        jobTypeNot.add(1);
        jobTypeNot.add(2);
        jobTypeNot.add(3);
        jobTypeNot.add(5);
        QueryWrapper<EmployeeRecruitmentEntity> queryWrapper =  new QueryWrapper<EmployeeRecruitmentEntity>().like(!StringUtils.isEmpty(name),"name",name)
                .in(!CollectionUtils.isEmpty(isRegular),"is_regular",isRegular)
                .eq(dealStatus!=null,"deal_status",dealStatus)
                .eq(isDistribute!=null,"is_distribute",isDistribute)
                .eq("is_delete",0).in("is_reinstate",integers)
                .in(!CollectionUtils.isEmpty(regularBpmStatus),"regular_bpm_status",regularBpmStatus)
                .in(!ObjectUtils.isEmpty(lineSystem),"line_id",lineSystem).in(!ObjectUtils.isEmpty(jobSystem),"job_system_id",jobSystem)
                .eq("is_entry_status",1).le(flag!=null&&flag,"regular_day", DateUtils.addDateDays(new Date(),-20))
                //查询转正未处理数据时不显示离职人员
                .ne(flag!=null&&flag,"employeestatus",3)
                //不查询实习生
                .ne("employeestatus","9")
                .notIn("job_type",jobTypeNot)
                .in(!CollectionUtils.isEmpty(org),"org_id",org)
                .eq(org!=null&&org.size()==0,"org_id",null)
                .in(!CollectionUtils.isEmpty(unit),"unit_id",unit)
                .eq(unit!=null&&unit.size()==0,"unit_id",null)
                .in(!CollectionUtils.isEmpty(job),"job_id",job)
                .eq(job!=null&&job.size()==0,"job_id",null);
        queryWrapper.and(wra->wra.in(!CollectionUtils.isEmpty(orgIds),"unit_id",orgIds).or().
                eq("principal_user_code",regularVo.getCode()).or().eq(CollectionUtils.isEmpty(orgIds),"unit_id",null)) .orderByAsc("regular_day");

        return queryWrapper;
    }



    @Override
    public EmployeeRecruitmentEntity getByIdE(Long id) {
            EmployeeRecruitmentEntity one = this.getOne(new QueryWrapper<EmployeeRecruitmentEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(EmployeeRecruitmentEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(EmployeeRecruitmentEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            EmployeeRecruitmentEntity entity = new EmployeeRecruitmentEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}